package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author ZhangXiXi
 * @ClassName CheckItemServiceImpl
 * @Description TODO
 * @date 2021/1/5 15:45
 * @Version 1.0
 */
@DubboService
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;
    @Transactional
    @Override
    public void add(CheckItem checkItem) {
        log.info("[检查项:添加]data:{}", checkItem);
        //调用dao插入数据
        checkItemDao.insert(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[检查项:分页查询]data:{}", queryPageBean);
//        1 设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
//        2 查询数据
        Page<CheckItem> page = checkItemDao.selectByCondition(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }
    @Transactional
    @Override
    public void deleteById(Integer id) {
        log.info("[检查项:删除]id:{}", id);
        //检查是否有关联的检查组
        Long count=checkItemDao.countCheckgroupByCheckitemId(id);
        if (count>0){
            throw new BusinessRuntimeException("禁止操作,请删除关联数据后重试!");
        }
        checkItemDao.deleteById(id);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.selectById(id);
    }
@Transactional
    @Override
    public void edit(CheckItem checkItem) {
    log.info("[检查项:更新]data:{}", checkItem);
    checkItemDao.update(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        log.info("[检查项-查询所有]~");
        //调用dao查询
        return checkItemDao.selectAll();
    }
}
